Ext.SSL_SECURE_URL = "ui/resources/images/default/s.gif";
Ext.BLANK_IMAGE_URL = "ui/resources/images/default/s.gif";

var login = {
	submitUrl : "Login",
	pMask : null,
	init : function() {
		if (document.all) { // IE
			Ext.MessageBox.show({
						title : '警告',
						msg : "为了更好的使用本平台，请使用火狐浏览器登陆。",
						buttons : Ext.MessageBox.OK,
						icon : Ext.MessageBox.WARNING
					});
		} else { // FIREFOX
			if (opener) {
				opener.location.reload();
			}
		}
		var userName = document.getElementById("username");
		userName.onkeydown = function(event) {
			if (event && event.keyCode && event.keyCode == 13) {
				login.loginSystem();
			}
		}
		var password = document.getElementById("password");
		password.onkeydown = function(event) {
			if (event && event.keyCode && event.keyCode == 13) {
				login.loginSystem();
			}
		}
	},
	validateUserName : function() {
		var userName = document.getElementById("username");
		if (!userName.value || userName.value.match(/^\s*$/)) {
			Ext.MessageBox.show({
						title : '警告',
						msg : "用户名不能为空！",
						buttons : Ext.MessageBox.OK,
						icon : Ext.MessageBox.WARNING
					});
		}
	},
	validatePwd : function() {
		var password = document.getElementById("password");
		if (!password.value || password.value.match(/^\s*$/)) {
			Ext.MessageBox.show({
						title : '警告',
						msg : "密码不能为空！",
						buttons : Ext.MessageBox.OK,
						icon : Ext.MessageBox.WARNING
					});
		}
	},
	loginSystem : function() {
		var title = "";
		var validFlag = true;
		var userName = document.getElementById("username");
		var nameValue = userName.value;
		if (!nameValue || nameValue.match(/^\s*$/)) {
			title += "用户名";
			validFlag = false;
		}
		var password = document.getElementById("password");
		var pwdValue = password.value;
		if (!pwdValue || pwdValue.match(/^\s*$/)) {
			if (!validFlag) {
				title += ",";
			} else {
				validFlag = false;
			}
			title += "密码"
		}
		if (!validFlag) {
			title += "不能为空！";
			Ext.MessageBox.show({
						title : '警告',
						msg : title,
						buttons : Ext.MessageBox.OK,
						icon : Ext.MessageBox.WARNING
					});
			return;
		}
		this.showMask();
		var loginButton = document.getElementById("loginButton");
		Ext.Ajax.request({
					url : login.submitUrl,
					method : "POST",
					params : {
						username : nameValue,
						password : pwdValue,
						hashcode : '1'
					},
					success : function(response) {
						var result = Ext.JSON.decode(response.responseText);
						if (!result.success) {
							login.hideMask();
							Ext.Msg.show({
										title : '操作提示',
										msg : result.errors.message,
										buttons : Ext.MessageBox.OK,
										icon : Ext.MessageBox.ERROR
									});
						} else {
							window.location.href = 'main.jsp';
						}
					},
					failure : function(response) {
						login.hideMask();
						Ext.MessageBox.show({
									title : '失败',
									msg : '服务器连接失败，请稍候再试！',
									buttons : Ext.MessageBox.OK,
									icon : Ext.MessageBox.ERROR
								});
					}
				});
	},
	showMask : function(msg) {
		if (!this.pMask) {
			this.pMask = new Ext.LoadMask(Ext.getBody(), {
						msg : '正在验证，请稍候......'
					});
		}
		if (msg) {
			this.pMask.msg = msg;
		}
		this.pMask.show();
	},
	hideMask : function() {
		this.pMask.hide();
	}
};
